//
//  FXCNewViewController.m
//  XinLangFeiBo
//
//  Created by dllo on 16/3/3.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FXCNewViewController.h"
#import "FXCNewTableViewController.h"

@interface FXCNewViewController ()<UIScrollViewDelegate>
/**
 *  标题栏
 */
@property (nonatomic, retain) UIScrollView *smallScrollView;
/**
 *  内容栏
 */
@property (nonatomic, retain) UIScrollView *bigScrollView;

/**
 *  新闻接口数组
 */
@property (nonatomic, retain)NSArray *arrayLists;



@end

@implementation FXCNewViewController


/**
 *  懒加载数组
 */
- (NSArray *)arrayLists{
    if (_arrayLists == nil) {
         _arrayLists = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"NewsURLs.plist" ofType:nil]];
    }
    return _arrayLists;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _smallScrollView = [[UIScrollView alloc] init];
    _smallScrollView.showsHorizontalScrollIndicator = NO;
    _smallScrollView.bounces = NO;
//    _smallScrollView.backgroundColor = [UIColor yellowColor];
    _smallScrollView.frame = CGRectMake(0, 64, self.view.width, 50);
    _smallScrollView.contentSize = CGSizeMake(self.arrayLists.count * 70, 0);
    
    [self.view addSubview:_smallScrollView];
    
    [self addLable];
    
    
    _bigScrollView = [[UIScrollView alloc] init];
//    _bigScrollView.backgroundColor = [UIColor blueColor];
    _bigScrollView.contentSize = CGSizeMake(self.view.width * self.arrayLists.count, 0);
    _bigScrollView.pagingEnabled = YES;
    [self.view addSubview:_bigScrollView];
    _bigScrollView.sd_layout
    .topSpaceToView(_smallScrollView, 0)
    .leftEqualToView(_smallScrollView)
    .rightEqualToView(_smallScrollView)
    .heightIs(self.view.height - self.smallScrollView.height - 113);
    _bigScrollView.delegate = self;
    [self addController];
    
    
//    添加默认控制器
    UIViewController *vc = [self.childViewControllers firstObject];
    vc.view.frame = self.bigScrollView.bounds;
    [self.bigScrollView addSubview:vc.view];
}

- (void)addController{
    for (int i = 0; i < self.arrayLists.count; i++) {
        FXCNewTableViewController *vc = [FXCNewTableViewController new];
        vc.title = self.arrayLists[i][@"title"];
        vc.urlString = self.arrayLists[i][@"urlString"];
        [self addChildViewController:vc];
    }
}

- (void)addLable{
    for (int i = 0; i < self.arrayLists.count; i++) {
        UILabel *label = [[UILabel alloc] init];
        label.text = self.arrayLists[i][@"title"];
        label.frame = CGRectMake(i * 70, 0, 70, 40);
        label.font = [UIFont fontWithName:@"HYQiHei" size:19];
//        打开用户交互
        label.userInteractionEnabled = YES;
        label.textAlignment = NSTextAlignmentCenter;
        [self.smallScrollView addSubview:label];
        label.tag = 1000 + i;
        [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick:)]];
        
    }
}



- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    //    索引
    NSUInteger index = scrollView.contentOffset.x / self.view.width;
    //    标题滚动关联
    UILabel *label = self.smallScrollView.subviews[index];
    
    CGFloat offsetx = label.center.x - self.smallScrollView.frame.size.width * 0.5;
    
    CGFloat offsetMax = self.smallScrollView.contentSize.width - self.smallScrollView.frame.size.width;
    if (offsetx < 0) {
        offsetx = 0;
    }else if (offsetx > offsetMax){
        offsetx = offsetMax;
    }
    
    CGPoint offset = CGPointMake(offsetx, self.smallScrollView.contentOffset.y);
    [self.smallScrollView setContentOffset:offset animated:YES];
    //    移除所有子视图
    [self.bigScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    //    NSLog(@"%f", _smallScrollView.contentOffset.x);
    
    //    添加控制器
    FXCNewTableViewController *newsVC = self.childViewControllers[index];
    newsVC.view.frame = self.bigScrollView.bounds;
    [self.bigScrollView addSubview:newsVC.view];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self scrollViewDidEndScrollingAnimation:scrollView];

}


- (void)labelClick:(UITapGestureRecognizer *)recognizer{
    UILabel *label = (UILabel *)recognizer.view;
    CGFloat offsetX = (label.tag - 1000) * self.bigScrollView.width;
//    CGFloat offsetY = 0;
//    label.textColor = [UIColor redColor];
//    
//    UILabel *frontLabel = [self.smallScrollView viewWithTag:label.tag - 1];
//    frontLabel.textColor = [UIColor blackColor];
//    
//    UILabel *nextLabel = [self.smallScrollView viewWithTag:label.tag + 1];
//    nextLabel.textColor = [UIColor blackColor];
    
    CGPoint offset = CGPointMake(offsetX, 0);
    [self.bigScrollView setContentOffset:offset animated:YES];
    
    
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
